 class Myblockqueue {
    public String[] value = null;
    private int head=0;
    private int tail=0;
    private int size = 0;
    private Object lock = new Object();
    public void put (String s) throws InterruptedException {
        synchronized (lock){
            while(size >= value.length){
                //队列满，阻塞
                lock.wait();
            }
            value[tail] = s;
            size++;
            tail++;
            if(tail>=value.length){
                tail = 0;
            }
            lock.notify();
        }

    }
    public String take () throws InterruptedException {
        synchronized (lock){
            while(size == 0){
                lock.wait();
                //队列空，阻塞

            }
            String str = value[head];
            head++;
            lock.notify();
            if(head>=value.length){
                head = 0;
            }
            size--;
            return str;
        }
    }
    public Myblockqueue(int num){
        value = new String[num];
    }
}
public class MyblockqueueTest {
    public static void main(String[] args) throws InterruptedException {
        Myblockqueue q1 = new Myblockqueue(3);
        Thread t1 = new Thread(()->{
            int n = 0;
           while(true) {
               try {
                   q1.put(n+"");
                   System.out.println("生产者队列"+n);
                   n++;
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        Thread t2 = new Thread(()->{
            String n;
            while(true) {
                try {
                    n = q1.take();
                    System.out.println("消费者队列"+n);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();

    }
}

